import { ESGeoExtrudedPolygon, ESGeoPolygon, ESJEditingMode } from "earthsdk3";
import { ElMessage } from "element-plus";
import { PartialWithUndefinedReactivePropsToNativeProps, ReactivePropsToNativePropsAndChanged, SceneObjectKey, extendClassProps, reactJson } from "earthsdk3";

/**
 * 测试 rts使用的多形体
 */
export class ESRtsPolygon extends ESGeoPolygon {
    static override readonly type = this.register('ESRtsPolygon', this, { chsName: 'rts使用的地理多边形', tags: ['ESObjects', '_ES_Impl_Cesium', '_ES_Impl_UE'], description: "地理多边形体" });
    override get typeName() { return 'ESRtsPolygon'; }
    override get defaultProps() { return ESRtsPolygon.createDefaultProps(); }
    override get json() {
        return {
            // @ts-ignore
            ...this._innerGetJson(),
            "show": this.show,
            "props": this.props,
            "materials": this.materials,
        } as JsonType;
    }
    override set json(value: JsonType) { this._innerSetJson(value); }

    static override supportEditingModes: string[] = [
        ESJEditingMode.CircularAppend,
        ESJEditingMode.CircularInsert,
        ESJEditingMode.Translation,
        ...ESGeoPolygon.supportEditingModes
    ];

    override updateEditing() {
        const mode = [
            ESJEditingMode.CircularAppend,
            ESJEditingMode.Translation,
            ESJEditingMode.CircularInsert
        ]
        if (this.points && this.points.length > 2) {
            mode.reverse();
        }
        this.activeViewer?.startEditing(this, mode, { callSource: 'inner' });
    }

    constructor(id?: SceneObjectKey) {
        super(id);
        // this.show = false

        {
            const updata = () => {
                this.rgbaColor = this.fillColor.map((e: number, i: number) => {
                    if (i === 3) {
                        return e;
                    }
                    return e * 255 || 0
                })
            }
            this.dispose(this.fillColorChanged.disposableOn(updata))
        }

        {
            let message: any
            const updata = () => {
                if (this.editing) {
                    message = ElMessage({
                        message: '1. 双击鼠标左键或点击ESC键退出编辑2. 点击空格键进行编辑方式的切换',
                        duration: 0,
                        plain: true,
                    })
                } else {
                    message && message.close()
                }
            }
            this.dispose(this.editingChanged.disposableOn(updata))
            this.dispose(() => message && message.close())
        }
    }
    override getESProperties() {
        const properties = { ...super.getESProperties() };
        return {
            ...properties,
            coordinate: [
                ...properties.coordinate,
            ]
        };
    };
    override getProperties(language: string) {
        return [
            ...super.getProperties(language),
        ];
    }
}

export namespace ESRtsPolygon {
    export const createDefaultProps = () => ({
        ...ESGeoExtrudedPolygon.createDefaultProps(),
        datasetName: undefined as string | undefined,
        featureClassId: undefined as string | undefined,
        tileServiceName: undefined as string | undefined,
        isHeightEqual: undefined as boolean | undefined,
        create: undefined as boolean | undefined,
        featureId: undefined as string | undefined,
        geometryId: undefined as string | undefined,
        materials: reactJson<any>({}),
        rgbaColor: reactJson<any>([1, 1, 1, 0.4]),
        uiShowprops: reactJson<any>({}),
        props: reactJson<any>({}),

    });
}
extendClassProps(ESRtsPolygon.prototype, ESRtsPolygon.createDefaultProps);
export interface ESRtsPolygon extends ReactivePropsToNativePropsAndChanged<ReturnType<typeof ESRtsPolygon.createDefaultProps>> { }
type JsonType = PartialWithUndefinedReactivePropsToNativeProps<ReturnType<typeof ESRtsPolygon.createDefaultProps> & { type: string }>;